// src/hooks/useDebounce.ts

import { useState, useEffect } from 'react'

/**
 * 简单的防抖 Hook
 * @param value   需要防抖的值
 * @param delay   延迟时间，默认 500ms
 */
export function useDebounce<T>(value: T, delay = 500): T {
  const [debouncedVal, setDebouncedVal] = useState<T>(value)

  useEffect(() => {
    const handler = setTimeout(() => {
      setDebouncedVal(value)
    }, delay)

    return () => {
      clearTimeout(handler)
    }
  }, [value, delay])

  return debouncedVal
}
